<?php

if(!isset($_SESSION['username'])){
		echo "<script>alert('请登录！');window.location.href='login.php?m=admin&c=manage'</script>";die;
	}
	//权限管理
	authonity($con);
	$mid = $_GET['mid'];

	$aid = $_GET['aid'];
//提交
	if($_POST){
//		var_dump($_POST) ; die;
		if($_POST['name'] == ''){
			echo "<script>alert('名称不可为空');</script>";
		}elseif($_POST['identification_pro'] == ''){
			echo "<script>alert('产品编号不可为空');</script>";
		}elseif($_POST['intro']==''){
			echo "<script>alert('产品介绍不可为空');</script>";
		}else{
			$con = connect('localhost','root','','login');
			//获取数据
			$data['name'] = htmlspecialchars(trim($_POST['name']));
			$data['identification_pro'] = htmlspecialchars(trim($_POST['identification_pro']));
			$data['intro'] = htmlspecialchars_decode(trim($_POST['intro']));
			// $data['edit'] = htmlspecialchars(trim($_POST['edit']))
			$data['launch_time'] = time();
			$data['category_id'] = $_POST['category_id'];



			//图片上传
			if(isset($_FILES['pro_img'])){
				$pro_img = $_FILES['pro_img'];
				$img = uploadsIMG($pro_img);

				$ho_thumb = thumb($img,250,250,'home');

				$ad_thumb = thumb($img,50,50);

			}

			//详情图片
			$detail1_img = $_FILES['detail1'];
			$detail2_img = $_FILES['detail2'];
			$detail3_img = $_FILES['detail3'];

			

			$detail1 = uploadsIMG($detail1_img);

			if($detail2_img['size'] != 0){

				$detail2 = uploadsIMG($detail2_img);

				if($detail3_img['size'] != 0){

					$detail3 = uploadsIMG($detail3_img);

				}
			}
			

			if(isset($detail1)){
				$img_detail[] = $detail1;
				if(isset($detail2)){
					$img_detail[] = $detail2;
					if(isset($detail3)){
						$img_detail[] = $detail3;
					}
				}

				$data['img_detail'] = json_encode($img_detail);
			}

			//产品图片
			$img_path[] = $img;
			$img_path[] = $ho_thumb;
			$img_path[] = $ad_thumb;

			$data['img_path'] = json_encode($img_path);

			//产品表插入
			$query = dbinsert($con,'products',$data);


			$size[] = htmlspecialchars(trim($_POST['height'],''));
			$size[] = htmlspecialchars(trim($_POST['width'],''));
			$size[] = htmlspecialchars(trim($_POST['deep'],''));

			$pro_mess['size'] = json_encode($size);
			$pro_mess['merit'] = htmlspecialchars(trim($_POST['merit']));
			$pro_mess['ingredient'] = htmlspecialchars_decode(trim($_POST['ingredient']));
			$pro_mess['filler'] = htmlspecialchars(trim($_POST['filler']));

			$pro_id = dbselect($con,'products','id','','launch_time desc','1');

			
			$pro_mess['pro_id'] = $pro_id[0]['id'];
			$mess_query = dbinsert($con,'pro_message',$pro_mess);

			if($query){
			echo "<script>alert('添加成功');window.location.href = 'index.php?m=admin&c=product&mid=17&aid=$aid'</script>";
		}else{
			echo "<script>alert('添加失败');window.location.href = 'index.php?m=admin&c=product&v=pro_insert&mid=$mid&aid=$aid'</script>";
		}
}
}		//添加
	




	$pro_cate = dbselect($con,'category','category_name,id','pid=4');	


	assign('pro_cate',$pro_cate);






?>